# -*- coding: utf-8 -*-
require 'test/unit'
require 'm3g'

class Test_VertexBuffer < Test::Unit::TestCase
  def setup
  end

  def teadown
  end

  def test_default_variables
    vbuf = M3G::VertexBuffer.new

    assert_equal 0xffffffff, vbuf.default_color
    assert_equal nil       , vbuf.colors[0]
    assert_equal nil       , vbuf.normals[0]
    assert_equal nil       , vbuf.positions[0]
    assert_equal nil       , vbuf.tex_coords[0][0]
    assert_equal nil       , vbuf.tex_coords[1][0]
    assert_equal nil       , vbuf.tex_coords[2][0]
    assert_equal nil       , vbuf.tex_coords[3][0]
    assert_equal 0         , vbuf.vertex_count
  end


  def test_set_variables
    vbuf       = M3G::VertexBuffer.new
    colors     = M3G::VertexArray.new 1, 3, 1
    normals    = M3G::VertexArray.new 1, 3, 2
    positions  = M3G::VertexArray.new 1, 3, 2
    tex_coords = M3G::VertexArray.new 1, 2, 2

    vbuf.default_color = 0xfe345678
    vbuf.colors        = colors
    vbuf.normals       = normals
    vbuf.positions     = positions , 10, [1,2,3]
    vbuf.tex_coords[1] = tex_coords, 10, [1,2]

    assert_equal 0xfe345678, vbuf.default_color
    assert_equal 1         , vbuf.vertex_count

    varry, scale, bias = vbuf.positions
    assert_equal    positions , varry
    assert_in_delta 10.0      , scale  , 0.00001
    assert_in_delta 1.0       , bias[0], 0.00001
    assert_in_delta 2.0       , bias[1], 0.00001
    assert_in_delta 3.0       , bias[2], 0.00001

    varry, scale, bias = vbuf.colors
    assert_equal    colors    , varry
    assert_in_delta 1/255.0   , scale  , 0.00001
    assert_in_delta 0         , bias[0], 0.00001
    assert_in_delta 0         , bias[1], 0.00001
    assert_in_delta 0         , bias[2], 0.00001

    varry, scale, bias = vbuf.normals
    assert_equal    normals   , varry
    assert_in_delta 2/65535.0 , scale  , 0.00001
    assert_in_delta 1/65535.0 , bias[0], 0.00001
    assert_in_delta 1/65535.0 , bias[1], 0.00001
    assert_in_delta 1/65535.0 , bias[2], 0.00001

    # 仕様上tex_coordsがセットされていない時のscale, biasは未定義.
    # Desktop-M3Gの実装では0が入っている。

    varry, scale, bias = vbuf.tex_coords[0]
    assert_equal    nil       , varry
    assert_in_delta 0         , scale  , 0.00001
    assert_in_delta 0         , bias[0], 0.00001
    assert_in_delta 0         , bias[1], 0.00001

    varry, scale, bias = vbuf.tex_coords[1]
    assert_equal    tex_coords, varry
    assert_in_delta 10.0      , scale  , 0.00001
    assert_in_delta 1.0       , bias[0], 0.00001
    assert_in_delta 2.0       , bias[1], 0.00001

    varry, scale, bias = vbuf.tex_coords[2]
    assert_equal    nil       , varry
    assert_in_delta 0         , scale  , 0.00001
    assert_in_delta 0         , bias[0], 0.00001
    assert_in_delta 0         , bias[1], 0.00001
  end


  def test_duplicate
    vbuf1      = M3G::VertexBuffer.new
    colors     = M3G::VertexArray.new 1, 3, 1
    normals    = M3G::VertexArray.new 1, 3, 2
    positions  = M3G::VertexArray.new 1, 3, 2
    tex_coords = M3G::VertexArray.new 1, 2, 2

    vbuf1.default_color = 0xfe345678
    vbuf1.colors        = colors
    vbuf1.normals       = normals
    vbuf1.positions     = positions , 10, [1,2,3]
    vbuf1.tex_coords[1] = tex_coords, 10, [1,2]

    vbuf2 = vbuf1.duplicate

    assert_not_equal  vbuf1, vbuf2
    assert_equal 0xfe345678, vbuf2.default_color
    assert_equal 1         , vbuf2.vertex_count

    varry, scale, bias = vbuf2.positions
    assert_equal    positions , varry
    assert_in_delta 10.0      , scale  , 0.00001
    assert_in_delta 1.0       , bias[0], 0.00001
    assert_in_delta 2.0       , bias[1], 0.00001
    assert_in_delta 3.0       , bias[2], 0.00001

    varry, scale, bias = vbuf2.colors
    assert_equal    colors    , varry
    assert_in_delta 1/255.0   , scale  , 0.00001
    assert_in_delta 0         , bias[0], 0.00001
    assert_in_delta 0         , bias[1], 0.00001
    assert_in_delta 0         , bias[2], 0.00001

    varry, scale, bias = vbuf2.normals
    assert_equal    normals   , varry
    assert_in_delta 2/65535.0 , scale  , 0.00001
    assert_in_delta 1/65535.0 , bias[0], 0.00001
    assert_in_delta 1/65535.0 , bias[1], 0.00001
    assert_in_delta 1/65535.0 , bias[2], 0.00001

    varry, scale, bias = vbuf2.tex_coords[0]
    assert_equal    nil       , varry
    assert_in_delta 0         , scale  , 0.00001
    assert_in_delta 0         , bias[0], 0.00001
    assert_in_delta 0         , bias[1], 0.00001

    varry, scale, bias = vbuf2.tex_coords[1]
    assert_equal    tex_coords, varry
    assert_in_delta 10.0      , scale  , 0.00001
    assert_in_delta 1.0       , bias[0], 0.00001
    assert_in_delta 2.0       , bias[1], 0.00001

    varry, scale, bias = vbuf2.tex_coords[2]
    assert_equal    nil       , varry
    assert_in_delta 0         , scale  , 0.00001
    assert_in_delta 0         , bias[0], 0.00001
    assert_in_delta 0         , bias[1], 0.00001
  end

  def references
    vbuf        = M3G::VertexBuffer.new
    colors      = M3G::VertexArray.new 1, 3, 1
    normals     = M3G::VertexArray.new 1, 3, 2
    positions   = M3G::VertexArray.new 1, 3, 2
    tex_coords0 = M3G::VertexArray.new 1, 2, 2
    tex_coords1 = M3G::VertexArray.new 1, 2, 2
    tex_coords2 = M3G::VertexArray.new 1, 2, 2
    tex_coords3 = M3G::VertexArray.new 1, 2, 2
    
    vbuf.colors        = colors
    vbuf.normals       = normals
    vbuf.positions     = positions  , 3, [1,2,3]
    vbuf.tex_coords[0] = tex_coords0, 3, [1,2,3]
    vbuf.tex_coords[1] = tex_coords1, 3, [1,2,3]
    vbuf.tex_coords[2] = tex_coords2, 3, [1,2,3]
    vbuf.tex_coords[3] = tex_coords3, 3, [1,2,3]

    objs = vbuf.references
    assert_equal 7            , objs.length
    assert_equal colors       , objs[0]
    assert_equal normals      , objs[1]
    assert_equal positions    , objs[2]
    assert_equal tex_coords[0], objs[3]
    assert_equal tex_coords[1], objs[4]
    assert_equal tex_coords[2], objs[5]
    assert_equal tex_coords[3], objs[6]
  end


end
